package com.coai.twin.controller

import com.coai.twin.service.*
import kotlinx.coroutines.runBlocking
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/digital-twin")
class DigitalTwinController(
    private val simulationEngine: SimulationEngine
) {
    
    /**
     * 获取实时KPI
     */
    @GetMapping("/kpis/realtime")
    fun getRealTimeKPIs(): Map<String, Any> {
        val kpis = simulationEngine.getRealTimeKPIs()
        return mapOf(
            "success" to true,
            "data" to kpis
        )
    }
    
    /**
     * What-If分析
     */
    @PostMapping("/simulation/what-if")
    fun runWhatIfAnalysis(@RequestBody scenario: SimulationScenario): Map<String, Any> = runBlocking {
        val result = simulationEngine.runWhatIfAnalysis(scenario)
        mapOf(
            "success" to true,
            "data" to result
        )
    }
    
    /**
     * 运行模拟
     */
    @PostMapping("/simulation/run")
    fun runSimulation(
        @RequestBody parameters: Map<String, Double>,
        @RequestParam duration: Int
    ): Map<String, Any> = runBlocking {
        val steps = simulationEngine.runSimulation(parameters, duration)
        mapOf(
            "success" to true,
            "data" to steps
        )
    }
}
